package com.ithm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithm.reggie.common.CustomException;
import com.ithm.reggie.entity.Category;
import com.ithm.reggie.entity.Dish;
import com.ithm.reggie.entity.Setmeal;
import com.ithm.reggie.service.CategoryService;
import com.ithm.reggie.mapper.CategoryMapper;
import com.ithm.reggie.service.DishService;
import com.ithm.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 86182
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2024-08-10 10:31:50
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();


        //查询当前分类是否关联了菜品，是返回业务异常
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1>0){
            throw new CustomException("当前分类下关联了菜品，删除失败");
        }

        //查询当前分类是否关联了套餐，是返回业务异常
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2>0){
            throw new CustomException("当前分类下关联了套餐，删除失败");
        }

        //删除分类
        super.removeById(id);
    }
}




